﻿using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using ViewModels.Login;

namespace LogForParameter.Api.JWTAuth
{
    public class JWTTokenHelp
    {
        public static TokenInfoViewModel BuildJwtToken(Claim[] claims,PermissionRequirement permissionRequirement)
        {
            var now = DateTime.Now;
            var jwt = new JwtSecurityToken(
                issuer: permissionRequirement.Issuer,
                audience: permissionRequirement.Audience,
                claims: claims,
                notBefore:now,
                expires:now.Add(permissionRequirement.Expiration),
                signingCredentials:permissionRequirement.SigningCredentials
                ) ;
            var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);
            var responseJson = new TokenInfoViewModel
            {
                success = true,
                token = encodedJwt,
                expires_in = permissionRequirement.Expiration.TotalSeconds
            };
            return responseJson;
        }
    }
}
